<template>
  <el-dialog
    :visible.sync="reset2PasswordVisible"
    :close-on-click-modal="false"
    :modal-append-to-body="false"
    title="重置密码"
    width="30%"
    @close="resetPasswordClose"
  >
    <div class="el-password">
      <el-form ref="passForm" :model="passForm" :rules="rules">
        <el-form-item label="输入密码" prop="password">
          <el-input
            v-model="passForm.password"
            type="password"
            placeholder="请输入密码"
            clearable
          />
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
      <el-button @click="resetPasswordClose">取 消</el-button>
      <el-button type="primary" @click="passSubmit(passForm)">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
export default {
  /**
   * 重置密码
   */
  name: "resetPassword",
  model: {
    prop: "passForm",
    event: "change",
  },
  props: ["resetPasswordVisible", "passForm"],
  data() {
    return {
      reset2PasswordVisible: false,
      rules: {
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^(?=.*[a-zA-Z])(?=.*\d).{6,12}$/,
            message: "密码需是6-12位之间的数字+字母组合",
          },
        ],
        username: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
        ],
      },
    };
  },
  watch: {
    resetPasswordVisible(val) {
      if (val) {
        this.reset2PasswordVisible = true;
      } else {
        this.reset2PasswordVisible = false;
      }
    },
  },
  methods: {
    // 重置密码 -- 关闭按钮
    resetPasswordClose() {
      this.$refs.passForm.resetFields();
      this.passForm.password = ''
      this.reset2PasswordVisible = false;
    },
    // 重置密码 --  提交
    passSubmit() {
      this.$refs.passForm.validate((valid) => {
        if (valid) {
          this.$emit("passSubmit", this.passForm);
        }
      });
    },
  },
};
</script>